//
//  MapSelectionLayer.cpp
//  DiceWar
//
//  Created by xiaofeng on 27/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include "MapSelectionLayer.h"
#include "GameMaster.h"
#include "SceneManager.h"
#include "GameUtil.h"

USING_NS_CC;

MapSelectionLayer::MapSelectionLayer()
: m_pHexMapLayer(NULL) {}

MapSelectionLayer::~MapSelectionLayer() {}

bool MapSelectionLayer::init()
{
    if (!CCLayer::init()) return false;
    if (!initHexMap()) return false;
    if (!initUserInterface()) return false;
    return true;
}

void MapSelectionLayer::onEnter()
{
    CCLayer::onEnter();
    
    GameMaster::instance().generateNextMap();
    m_pHexMapLayer->updateMapView();
}

bool MapSelectionLayer::initHexMap()
{
    m_pHexMapLayer = HexMapLayer::node();
    if (!m_pHexMapLayer) return false;
    addChild(m_pHexMapLayer);
    
    GameMaster& gameMaster = GameMaster::instance();
    gameMaster.generateNextMap();
    
    m_pHexMapLayer->updateMapView();
    return true;
}

bool MapSelectionLayer::initUserInterface()
{
    // initialize the end turn button
    CCMenuItemImage *pCloseItem = CCMenuItemImage::itemWithNormalImage("CloseNormal.png", "CloseSelected.png",  this, menu_selector(MapSelectionLayer::onNextMapButtonClicked));
    if (!pCloseItem) return false;
    pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );
    
    CCMenuItemImage* pStartButton = CCMenuItemImage::itemWithNormalImage("playButton.png", "playButtonDown.png", this, menu_selector(MapSelectionLayer::onStartGameButtonDown));
    if (!pStartButton) return false;
    pStartButton->setScale(0.2f);
    GameUtil::setRelativeToScreenPosition(pStartButton, ccp(0.9f, 0.3f));
    
    CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, pStartButton, NULL);
    if (!pMenu) return false;
    pMenu->setPosition(CCPointZero);
    
    addChild(pMenu, 2);
    
    return true;
}

void MapSelectionLayer::onNextMapButtonClicked(cocos2d::CCObject* pSender)
{
    GameMaster::instance().generateNextMap();
    m_pHexMapLayer->updateMapView();
}

void MapSelectionLayer::onStartGameButtonDown(cocos2d::CCObject* pSender)
{
    SceneManager::sharedSceneManager().switchToScene(SceneManager::kGameScene);
}